#ifndef SINAFRAME_H
#define SINAFRAME_H

#include "sina_global.h"
#include "ToolbarFrame.h"
#include "TweetFrame.h"
#include "UserViewerFrame.h"
#include "DirectMessageFrame.h"
#include <QPropertyAnimation>
#include <QNetworkRequest>
#include <QButtonGroup>
#include <QPushButton>
#include <QByteArray>
#include <QScrollBar>
#include <QObject>
#include <QMovie>
#include <QLabel>
namespace Sina
{
	class TweetFrame;
	class DataRepository;
	class SinaContextPrivate;
	class SinaFrame : public QFrame
	{
		Q_OBJECT
		public:
			explicit SinaFrame( SinaContext* , SinaContextPrivate* );
			void setMyselfIcon(const QPixmap& icon) { m_toolbar.m_mySelfBtn.setPixmap(icon); }
			void setWarningCount(ToolbarButton b,int c) { m_toolbar.setWarningCount(b,c); }
			void setLoadingView(ToolbarButton b, bool t) { t ? m_toolbar.showLoading(b) : m_toolbar.hideLoading(b); }

			void switchToView(MiniblogView v);

			void showLoading();
			void showError();
			void init(DataRepository*);
			void setMaxEntryCount(int c) { i_maxEntryCount = c; }

			void showUserDetail(const MiniblogUser& user) { m_userViewer.showUserDetail(user); }

			bool eventFilter(QObject* , QEvent* );
		public slots:
			void updateTimeline(const QMap<MiniblogId,MiniblogTweet>& totalTweets,const QVector<MiniblogId>& deletedDataIds);
			void updateDM(const DMRepository&);
			void dataUpdated(TweetCategory cate,int newCount,const QVector<MiniblogId>*);
		protected:
			void resizeEvent(QResizeEvent*);
		private:
			SinaContext*	m_context;
			SinaContextPrivate*	m_privateContext;
			DataRepository*		m_dataRepository;
			MiniblogView	e_currentView;
			int				i_maxEntryCount;

			// UIs
			QWidget			m_timelineWidget;
			QWidget			m_mentionWidget;
			QWidget			m_commentWidget;
			QWidget			m_dmWidget;
			QWidget			m_favoriteWidget;
			ToolbarFrame		m_toolbar;
			UserViewerFrame	m_userViewer;
			QScrollBar		m_scrollBar;
			QWidget*		m_scrollTarget;
			// Loadings
			QWidget*		m_errorWidget;
			QLabel*			m_errorLabel;
			QPushButton*	m_errorBtn;
			QMovie*			m_loadingMovie;

			QPropertyAnimation		m_animation;
			QPropertyAnimation		m_animation2;

			QMap<MiniblogId,TweetFrame*>	 m_timelineTweetFrames;
			QMap<MiniblogId,TweetFrame*> m_favoriteTweetFrames;
			QList<TweetFrame*>			 m_recycleTweetFrames;
			QMap<MiniblogId,DirectMessageFrame*> m_directMessageFrames;


			void updateScrollBar();
			void ensureChildrenWidth();

			QWidget*	widgetForView(MiniblogView v);
			TweetFrame* getTweetFrame() { return m_recycleTweetFrames.length() > 0 ? m_recycleTweetFrames.takeLast():new TweetFrame(m_context); }

			void updateViewFavorite(int newCount,const QVector<MiniblogId>*);
		private slots:
			void scrollView(int value) { m_scrollTarget->move(m_scrollTarget->x(),-value); }
			void toolbarButtonClicked(int);
			void animationFinished();

			void layoutTimeline(MiniblogId);
			void layoutFavorite(MiniblogId);
	};
} // namespace Sina

#endif // SINAFRAME_H
